package jeecg.business.controller.message;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.message.MessageEntity;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.business.service.message.MessageServiceI;
import jeecg.system.pojo.base.TSUser;
import jeecg.system.service.SystemService;
import jeecg.system.service.UserService;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Expression;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 消息
 * @author LinYingPiaoJian
 * @date 2015-07-20 08:55:40
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/messageController")
public class MessageController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MessageController.class);

	@Autowired
	private MessageServiceI messageService;
	@Autowired
	private SystemService systemService;
	@Autowired
	EnterServiceI enterService;
	@Autowired
	UserService userService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	
	/**
	 * 消息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "selfmessage")
	public ModelAndView selfmessage(HttpServletRequest request) {
		messageService.updateHasRead(getUser().getId());
		return new ModelAndView("jeecg/business/message/selfmessageList");
	}
	
	@RequestMapping(params = "selfdatagrid")
	public void selfdatagrid(MessageEntity message,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MessageEntity.class, dataGrid);
		//查询条件组装器
		/**
		 * 只能查看与自己相关的
		 */
		if(message == null) message = new MessageEntity();
		cq.or(Expression.eq("jsrid", getUser().getId()), Expression.eq("fsrid", getUser().getId()));
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, message);
		this.messageService.getDataGridReturn(cq, true);
		
		List<MessageEntity> mlist = dataGrid.getReaults();
		if(mlist != null){
			for(MessageEntity m : mlist)
				m.setFsnr(StringUtil.removeHTMLLable(m.getFsnr()));
		}
		
		TagUtil.datagrid(response, dataGrid);
	}
	
	@RequestMapping(params = "allmessage")
	public ModelAndView allmessage(HttpServletRequest request) {
		request.setAttribute("uid", getUser().getId());
		messageService.updateHasRead(getUser().getId());
		return new ModelAndView("jeecg/business/message/allmessageList");
	}

	/**
	 * 消息列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "message")
	public ModelAndView message(HttpServletRequest request) {
		request.setAttribute("qyid", request.getParameter("qyid"));
		String qyid = request.getParameter("qyid");
		if(StringUtil.isNotEmpty(qyid)){
			EnterEntity enter = systemService.get(EnterEntity.class, request.getParameter("qyid"));
			TSUser user = userService.queryUserByName(enter.getAccount());
			request.setAttribute("uid", user.getId());
		}
		return new ModelAndView("jeecg/business/message/messageList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MessageEntity message,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MessageEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, message);
		
		String qyid = request.getParameter("qyid");
		if(StringUtil.isNotEmpty(qyid)){
			EnterEntity enter = systemService.get(EnterEntity.class, request.getParameter("qyid"));
			TSUser user = userService.queryUserByName(enter.getAccount());
			cq.or(Expression.eq("jsrid", user.getId()), Expression.eq("fsrid", user.getId()));
		}
		
		this.messageService.getDataGridReturn(cq, true);
		List<MessageEntity> mlist = dataGrid.getReaults();
		if(mlist != null){
			for(MessageEntity m : mlist)
				m.setFsnr(StringUtil.removeHTMLLable(m.getFsnr()));
		}
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除消息
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MessageEntity message, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = systemService.getEntity(MessageEntity.class, message.getId());
		String omessage = "删除成功";
		messageService.delete(message);
		systemService.addLog(omessage, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		j.setMsg(omessage);
		return j;
	}


	/**
	 * 添加消息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MessageEntity message, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String omessage;
		if (StringUtil.isNotEmpty(message.getId())) {
			omessage = "更新成功";
			MessageEntity t = messageService.get(MessageEntity.class, message.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(message, t);
				messageService.saveOrUpdate(t);
				systemService.addLog(omessage, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			omessage = "添加成功";
			messageService.save(message);
			systemService.addLog(omessage, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}
	
	@RequestMapping(params = "addorupdateself")
	public ModelAndView addorupdateself(MessageEntity message, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(message.getId())) {
			message = messageService.getEntity(MessageEntity.class, message.getId());
		}else{
			message = new MessageEntity();
			message.setFsrid(getUser().getId());
			message.setFsr(getUser().getRealName());
			message.setJsr("管理员");
			message.setJsrid("40");
			message.setSj(new Date());
		}
		req.setAttribute("messagePage", message);
		return new ModelAndView("jeecg/business/message/selfmessage");
	}
	

	/**
	 * 消息列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MessageEntity message, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(message.getId())) {
			message = messageService.getEntity(MessageEntity.class, message.getId());
			req.setAttribute("messagePage", message);
		}else{
			String rid = req.getParameter("rid");
			if(StringUtil.isNotEmpty(rid)){
				MessageEntity omess = messageService.getEntity(MessageEntity.class, rid);
				message = new MessageEntity();
				message.setFsrid(omess.getJsrid());
				message.setFsr(omess.getJsr());
				message.setJsr(omess.getFsr());
				message.setJsrid(omess.getFsrid());
				message.setSj(new Date());
				req.setAttribute("messagePage", message);
			}
			
			String uid = req.getParameter("uid");
			if(StringUtil.isNotEmpty(uid)){
				TSUser user = userService.get(TSUser.class, uid);
				message = new MessageEntity();
				message.setFsrid(getUser().getId());
				message.setFsr(getUser().getRealName());
				message.setJsr(user.getRealName());
				message.setJsrid(user.getId());
				message.setSj(new Date());
				req.setAttribute("messagePage", message);
			}
			
		}
		return new ModelAndView("jeecg/business/message/message");
	}
}
